{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "73bd968b-d970-4a05-94ef-4e7abf990827",
   "metadata": {},
   "source": [
    "Chapter 14\n",
    "\n",
    "# 矩阵平方根\n",
    "Book_4《矩阵力量》 | 鸢尾花书：从加减乘除到机器学习 (第二版)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c263fc95-881a-49fb-9c6c-a25508996623",
   "metadata": {},
   "source": [
    "该代码的主要任务是通过矩阵分解和重构方法，基于给定的矩阵$A$，验证矩阵重构的正确性。具体流程如下：\n",
    "\n",
    "1. **定义矩阵$A$**：  \n",
    "   代码首先创建了一个$2 \\times 2$矩阵 $A = \\begin{bmatrix} 1.25 & -0.75 \\\\ -0.75 & 1.25 \\end{bmatrix}$。矩阵 $A$ 是一个对称矩阵，因此可以进行特征值分解。\n",
    "\n",
    "2. **计算特征值和特征向量**：  \n",
    "   代码使用 `np.linalg.eig` 函数对矩阵 $A$ 进行特征值分解，计算出$A$的特征值（存储在 $LAMBDA$ 中）和特征向量（存储在 $V$ 中），满足以下分解公式：\n",
    "   $$\n",
    "   A = V \\Lambda V^{-1}\n",
    "   $$\n",
    "   其中，$\\Lambda$ 是一个包含特征值的对角矩阵，$V$ 是由特征向量组成的矩阵。\n",
    "\n",
    "3. **构建矩阵$B$**：  \n",
    "   接下来，代码构建了一个新的矩阵 $B$。它的计算公式为：\n",
    "   $$\n",
    "   B = V \\sqrt{\\Lambda} V^{-1}\n",
    "   $$\n",
    "   其中，$\\sqrt{\\Lambda}$ 是对角矩阵，其对角元素是 $\\Lambda$ 的平方根。也就是说，$B$ 是通过将 $A$ 的特征值取平方根后重新组合得到的矩阵。\n",
    "\n",
    "4. **重构矩阵$A$并验证结果**：  \n",
    "   最后，通过矩阵 $B$ 构造了一个新矩阵 $A_{reproduced}$，其计算公式为：\n",
    "   $$\n",
    "   A_{reproduced} = B B^T\n",
    "   $$\n",
    "   这是利用矩阵 $B$ 重构 $A$ 的过程。对称矩阵 $A$ 的特征值平方根分解使得 $B B^T$ 应等于原始矩阵 $A$。因此，通过打印 $A_{reproduced}$，可以验证 $B B^T$ 是否等于 $A$，从而确认重构的正确性。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2759881c-9e2a-4e4d-a79c-1617f2a4be4f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np  # 导入NumPy库"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a685835a-bcda-40f8-ac57-ff3738c0209f",
   "metadata": {},
   "source": [
    "## 初始化矩阵A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a6e9a15c-1d77-4a95-a13c-1c825598e8be",
   "metadata": {},
   "outputs": [],
   "source": [
    "A = np.matrix([[1.25, -0.75],  # 定义矩阵A\n",
    "               [-0.75, 1.25]])  # 矩阵的元素"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "55297e89-757e-4136-a0af-4763d1db3e56",
   "metadata": {},
   "source": [
    "## 计算特征值和特征向量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "998fa920-5e90-408f-8b70-dd3633c870e9",
   "metadata": {},
   "outputs": [],
   "source": [
    "LAMBDA, V = np.linalg.eig(A)  # 计算矩阵A的特征值（LAMBDA）和特征向量（V）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "ce8e4f8c-19a0-4392-b1ef-e794dcc5f187",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2. , 0.5])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "LAMBDA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "13196da8-54c0-44bb-ac29-6cf7c6fec408",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[ 0.70710678,  0.70710678],\n",
       "        [-0.70710678,  0.70710678]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "V"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c6bb1a72-57de-4ae9-a2e2-599db3122538",
   "metadata": {},
   "source": [
    "## 构建矩阵B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "cb92d4fe-8443-473f-a61c-378630468024",
   "metadata": {},
   "outputs": [],
   "source": [
    "B = V @ np.diag(np.sqrt(LAMBDA)) @ np.linalg.inv(V)  # 根据特征值和特征向量构建矩阵B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "da0f2576-d262-41cd-8324-208cf3df1c82",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[ 1.06066017, -0.35355339],\n",
       "        [-0.35355339,  1.06066017]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "B"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d70a4893-0524-47aa-91ad-4ad9863a5c89",
   "metadata": {},
   "source": [
    "## 重构矩阵A并打印"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "d07c6472-787a-44c2-9d20-99e4d070826a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1.25 -0.75]\n",
      " [-0.75  1.25]]\n"
     ]
    }
   ],
   "source": [
    "A_reproduced = B @ B.T  # 通过矩阵B的转置乘积重构矩阵A\n",
    "print(A_reproduced)  # 输出重构后的矩阵A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "85a80909-2aac-49ed-bb7a-f8cc6b80ee7d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ecd322f4-f919-4be2-adc3-69d28ef25e69",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
